package com.gz.test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier;
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;

public class WxpayTest2 {
    private CloseableHttpClient httpClient;
    private AutoUpdateCertificatesVerifier verifier;

    // 你的商户私钥
    private static final String privateKey = "-----BEGIN PRIVATE KEY-----\n" +
            "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDBFRSLqrTDbgJg\n" +
            "u5GiYNnNhUySh/SryPxApiy7Hs7rHxNQMpNQIH1nXcOtjxaO4DZ7FPMdXmunus0J\n" +
            "UvK28/99nQ5vghyh17DClCI/84ZHPJlVhZs5vj9lmcowPoKu46QzmCi6lfdDQfyP\n" +
            "9j4lhZyTXd0R4ejVbCy284tkEDYtRGHZA2wUiSn2L91xXOQ8ODLhp82Tu9tjNUHU\n" +
            "+dCn5ZJMxbibw5d9Kp7JdoSpi07D5To4MD2CC0Gb+Bl7R/2AtYA4/u9RqOlsS3qY\n" +
            "vLO0O6nXPyAASGj0WahMnQHbgrVEEAHW2IWLg6u76CPVuPoAezyvxAv6xvwLvIRo\n" +
            "7gkbkE9/AgMBAAECggEAaah2eMNuba9Kf9LErB3hshSqxVi+rC6s01fcDH2cb4+u\n" +
            "yukSMfvFhAYMaXJuvrq1NX9OVpjvwnZ3cw3AIhu9iOikGKMTtgDJCoFziJljtqHT\n" +
            "qrgVLHEOUjPpL6NVcgzCMzWlvA0KpPR8F++BLJlECGSJ8Bo2hmvOwELf2V8BtpzM\n" +
            "tgKUB33/pDvDc61tu4HMgH6NgLvxpEuIWrrRAyyOUmA02fi+J4Is9k/ERBsbr2ga\n" +
            "W9VkWWhcSEkscxZ1En7yvmPYruz9oyj/hYDAKtlEiRIog3cPV/g4jWZ9SAVUYjXK\n" +
            "7WxmE/UsqI0hKok1171ljQ/gJSy2fV5oduBc2tCOMQKBgQDfwon8AdbzduVB6KHA\n" +
            "0VEJK1ut8Y4pNKqaQ+kvSWBQPjyuqyiH/a8YQUbqvkZ+lAiRkcJkIDqOnHsPtA/7\n" +
            "ncFiuQOdDriirmFZfYZnmzIfKsowPRHevUJOFHdihMYTwmpTaNQ8xit+EzNd7Ibu\n" +
            "6k2IA8sCK3DGTrJcBqkxbydOeQKBgQDc5v0NlB66XjsvGb0iVxVVT4NPQg70t0nQ\n" +
            "HgmdgepKJu4fUVbhyFyp08g/yPArRlst5MT6RtWpm0cuidg0E3q7+XFCaLPPpVGU\n" +
            "WaYOdFqoh9jEsWCmY8eqA4B7DSMIoRcM7LnXvalvo9qHif8qhiIwi8/T28P4bzC7\n" +
            "85AIJ8cvtwKBgEWV/U1m0Q8j8YX+r5cEQVxFpJxCm8BKxad16SWwfwhVYo+7YL5x\n" +
            "8Srs2f4UsF2a8dM7biWvNCa3mTm3+KR6WfK2sN39/wNwkYvAErVTuVGiAADibRVE\n" +
            "7ELV8I6t1d+9PU21MxO3WT07AWV7MjdInFXpQA/rrIWvYZ5m3+UkuSeZAoGBAJkq\n" +
            "bDF7D2dHqR+6aAKkgP06+Z/9/KK/Z6Wu/hnBF7M8lQUP//7jTyTN3F4ltCk9SkFj\n" +
            "CxVCAinsqYliOI+7JfGqPla+hFaomQ0Ur+f701RhhPK0zn/wVWPTs8UeUblNAM+M\n" +
            "YDtvnUj0QrSSAWP5x0ED4CFUkufyKMdZVPfipOC3AoGBALy8+SOWkHL9zQCZYQyi\n" +
            "AlHlB9bYCZoW1A8rTki+GxxeArtlBmAIQOyGrWh9Og6DRNUOpbmqARVztMpGvPBQ\n" +
            "SrpcbncfxsmB9rd1iE3GpZu9RfRbOFIwYNLx4xUFCTuBGbVy8a/LqxC0iYJGvqyN\n" +
            "/R2tu3VrMaFhqKikSmyOIOnF\n" +
            "-----END PRIVATE KEY-----";
    private static final String merchantId = "1651831173"; // 商户号
    private static final String merchantSerialNumber = "6C461D9DBD8B7ECB16E60DCD34DB832BFA7BEEF8"; // 商户证书序列号
    private static final String apiV3Key = "s1I6yGX7u38V8N0pYS4ga1qvef9EJbDH"; // API V3密钥

    @Before
    public void setup() {
        PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(privateKey);

        //使用自动更新的签名验证器，不需要传入证书
        verifier = new AutoUpdateCertificatesVerifier(
                new WechatPay2Credentials(merchantId, new PrivateKeySigner(merchantSerialNumber, merchantPrivateKey)),
                apiV3Key.getBytes(StandardCharsets.UTF_8));

// 初始化httpClient
        httpClient = WechatPayHttpClientBuilder.create().withMerchant(merchantId, merchantSerialNumber, merchantPrivateKey).withValidator(new WechatPay2Validator(verifier)).build();

    }
    @Test
    public  void createOrder() throws IOException {
        HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi");
//        HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/app");
        httpPost.addHeader("Accept", "application/json");
        httpPost.addHeader("Content-type","application/json; charset=utf-8");

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectMapper objectMapper = new ObjectMapper();

        ObjectNode rootNode = objectMapper.createObjectNode();
        rootNode.put("mchid","1651831173")
                .put("appid", "wxf063605c5a7e5e90")
                .put("description", "水达商品1")
                .put("notify_url", "https://www.baiduu.com")
                .put("out_trade_no", ""+System.currentTimeMillis())
        ;

        rootNode.putObject("amount")
                .put("total", 1);
        rootNode.putObject("payer")
                .put("openid", "oNa-S6_1Jrk54xQ8mPBLmMucr1HY");

        objectMapper.writeValue(bos, rootNode);

        httpPost.setEntity(new StringEntity(bos.toString("UTF-8"), "UTF-8"));
        CloseableHttpResponse response = httpClient.execute(httpPost);

        String bodyAsString = EntityUtils.toString(response.getEntity());
        System.out.println("测试结果----------->"+bodyAsString);
    }


}
